package com.dianping.cat.consumer.cross.model.transform;

import com.dianping.cat.consumer.cross.model.IEntity;
import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;










public class DefaultSaxParser
  extends DefaultHandler
{
/*  39 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  41 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  43 */   private Stack<String> m_tags = new Stack();

/*  45 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  49 */   private StringBuilder m_text = new StringBuilder();


/*  52 */   public static CrossReport parse(InputStream in) throws SAXException, IOException { return (CrossReport)parseEntity(CrossReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  56 */   public static CrossReport parse(Reader reader) throws SAXException, IOException { return (CrossReport)parseEntity(CrossReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  60 */   public static CrossReport parse(String xml) throws SAXException, IOException { return (CrossReport)parseEntity(CrossReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  66 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  67 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  69 */       factory.setValidating(false);
/*  70 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  72 */       factory.newSAXParser().parse(is, handler);
/*  73 */       return (T)handler.getEntity();
/*  74 */     } catch (ParserConfigurationException e) {
/*  75 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  80 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  84 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  90 */     if (obj instanceof String) {
/*  91 */       String str = (String)obj;

/*  93 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  94 */         return (T)str.substring(1);
      }
/*  96 */       return obj;
    }
/*  98 */     if (obj instanceof InputStream) {
/*  99 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 101 */       in.mark(3);

/* 103 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 104 */         in.reset();
      }

/* 107 */       return (T)in;
/* 108 */     }  if (obj instanceof Reader) {
/* 109 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 111 */       in.mark(1);

/* 113 */       if (in.read() != 65279) {
/* 114 */         in.reset();
      }

/* 117 */       return (T)in;
    }
/* 119 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 125 */     if (value == null || value.length() == 0) {
/* 126 */       return defaultValue;
    }

/* 129 */     if (type == Boolean.class)
/* 130 */       return (T)Boolean.valueOf(value); 
/* 131 */     if (type == Integer.class)
/* 132 */       return (T)Integer.valueOf(value); 
/* 133 */     if (type == Long.class)
/* 134 */       return (T)Long.valueOf(value); 
/* 135 */     if (type == Short.class)
/* 136 */       return (T)Short.valueOf(value); 
/* 137 */     if (type == Float.class)
/* 138 */       return (T)Float.valueOf(value); 
/* 139 */     if (type == Double.class)
/* 140 */       return (T)Double.valueOf(value); 
/* 141 */     if (type == Byte.class)
/* 142 */       return (T)Byte.valueOf(value); 
/* 143 */     if (type == Character.class) {
/* 144 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 146 */     return (T)value;
  }




/* 152 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 157 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 162 */     if (uri == null || uri.length() == 0) {
/* 163 */       Object currentObj = this.m_objs.pop();
/* 164 */       String currentTag = (String)this.m_tags.pop();

/* 166 */       if (currentObj instanceof CrossReport) {
/* 167 */         CrossReport crossReport = (CrossReport)currentObj;

/* 169 */         if ("domain".equals(currentTag)) {
/* 170 */           crossReport.addDomain(getText());
/* 171 */         } else if ("ip".equals(currentTag)) {
/* 172 */           crossReport.addIp(getText());
        }
      }
    }

/* 177 */     this.m_text.setLength(0);
  }


/* 181 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 185 */   protected String getText() { return this.m_text.toString(); }


  private void parseForCrossReport(CrossReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 189 */     if ("domain-names".equals(qName) || "domain".equals(qName) || "ips".equals(qName) || "ip".equals(qName)) {
/* 190 */       this.m_objs.push(parentObj);
/* 191 */     } else if ("local".equals(qName)) {
/* 192 */       Local local = this.m_maker.buildLocal(attributes);

/* 194 */       this.m_linker.onLocal(parentObj, local);
/* 195 */       this.m_objs.push(local);
    } else {
/* 197 */       throw new SAXException(String.format("Element(%s) is not expected under cross-report!", new Object[] { qName }));
    }

/* 200 */     this.m_tags.push(qName);
  }

  private void parseForLocal(Local parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 204 */     if ("remote".equals(qName)) {
/* 205 */       Remote remote = this.m_maker.buildRemote(attributes);

/* 207 */       this.m_linker.onRemote(parentObj, remote);
/* 208 */       this.m_objs.push(remote);
    } else {
/* 210 */       throw new SAXException(String.format("Element(%s) is not expected under local!", new Object[] { qName }));
    }

/* 213 */     this.m_tags.push(qName);
  }

  private void parseForName(Name parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 217 */     this.m_objs.push(parentObj);
/* 218 */     this.m_tags.push(qName);
  }

  private void parseForRemote(Remote parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 222 */     if ("type".equals(qName)) {
/* 223 */       Type type = this.m_maker.buildType(attributes);

/* 225 */       this.m_linker.onType(parentObj, type);
/* 226 */       this.m_objs.push(type);
    } else {
/* 228 */       throw new SAXException(String.format("Element(%s) is not expected under remote!", new Object[] { qName }));
    }

/* 231 */     this.m_tags.push(qName);
  }

  private void parseForType(Type parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 235 */     if ("name".equals(qName)) {
/* 236 */       Name name = this.m_maker.buildName(attributes);

/* 238 */       this.m_linker.onName(parentObj, name);
/* 239 */       this.m_objs.push(name);
    } else {
/* 241 */       throw new SAXException(String.format("Element(%s) is not expected under type!", new Object[] { qName }));
    }

/* 244 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 248 */     if ("cross-report".equals(qName)) {
/* 249 */       CrossReport crossReport = this.m_maker.buildCrossReport(attributes);

/* 251 */       this.m_entity = crossReport;
/* 252 */       this.m_objs.push(crossReport);
/* 253 */       this.m_tags.push(qName);
/* 254 */     } else if ("local".equals(qName)) {
/* 255 */       Local local = this.m_maker.buildLocal(attributes);

/* 257 */       this.m_entity = local;
/* 258 */       this.m_objs.push(local);
/* 259 */       this.m_tags.push(qName);
/* 260 */     } else if ("remote".equals(qName)) {
/* 261 */       Remote remote = this.m_maker.buildRemote(attributes);

/* 263 */       this.m_entity = remote;
/* 264 */       this.m_objs.push(remote);
/* 265 */       this.m_tags.push(qName);
/* 266 */     } else if ("type".equals(qName)) {
/* 267 */       Type type = this.m_maker.buildType(attributes);

/* 269 */       this.m_entity = type;
/* 270 */       this.m_objs.push(type);
/* 271 */       this.m_tags.push(qName);
/* 272 */     } else if ("name".equals(qName)) {
/* 273 */       Name name = this.m_maker.buildName(attributes);

/* 275 */       this.m_entity = name;
/* 276 */       this.m_objs.push(name);
/* 277 */       this.m_tags.push(qName);
    } else {
/* 279 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 285 */     if (uri == null || uri.length() == 0) {
/* 286 */       if (this.m_objs.isEmpty()) {
/* 287 */         parseRoot(qName, attributes);
      } else {
/* 289 */         Object parent = this.m_objs.peek();
/* 290 */         String tag = (String)this.m_tags.peek();

/* 292 */         if (parent instanceof CrossReport) {
/* 293 */           parseForCrossReport((CrossReport)parent, tag, qName, attributes);
/* 294 */         } else if (parent instanceof Local) {
/* 295 */           parseForLocal((Local)parent, tag, qName, attributes);
/* 296 */         } else if (parent instanceof Remote) {
/* 297 */           parseForRemote((Remote)parent, tag, qName, attributes);
/* 298 */         } else if (parent instanceof Type) {
/* 299 */           parseForType((Type)parent, tag, qName, attributes);
/* 300 */         } else if (parent instanceof Name) {
/* 301 */           parseForName((Name)parent, tag, qName, attributes);
        } else {
/* 303 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 307 */       this.m_text.setLength(0);
    } else {
/* 309 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 315 */       return (new SimpleDateFormat(format)).parse(str);
/* 316 */     } catch (ParseException e) {
/* 317 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 323 */       return (new DecimalFormat(format)).parse(str);
/* 324 */     } catch (ParseException e) {
/* 325 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/cross/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */